package com.flyfinger.codec.impl;

import com.alibaba.fastjson.JSON;
import com.flyfinger.codec.Serializer;

/**
 * @author zhouyp
 * @program code
 * @description
 * @create 2020-06-13
 */
public class JSONSerializer implements Serializer {

	private static volatile JSONSerializer SERIALIZER;

	private JSONSerializer() {
	}

	public static Serializer getInstance() {
		if (SERIALIZER == null) {
			synchronized (JSONSerializer.class) {
				if (SERIALIZER == null) {
					SERIALIZER = new JSONSerializer();
				}
			}
		}
		return SERIALIZER;
	}


	@Override
	public byte[] serialize(Object object) {
		return JSON.toJSONBytes(object);
	}

	@Override
	public <T> T deserialize(Class<T> clazz, byte[] bytes) {
		return JSON.parseObject(bytes, clazz);
	}

}
